package com.minsf.springbootinit.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minsf.springbootinit.exception.ThrowUtils;

import com.minsf.springbootinit.common.BaseResponse;
import com.minsf.springbootinit.common.ErrorCode;
import com.minsf.springbootinit.common.ResultUtils;
import com.minsf.springbootinit.exception.BusinessException;
import com.minsf.springbootinit.model.dto.questionSubmit.QuestionSubmitAddRequest;
import com.minsf.springbootinit.model.dto.questionSubmit.QuestionSubmitQueryRequest;
import com.minsf.springbootinit.model.entity.QuestionSubmit;
import com.minsf.springbootinit.model.entity.User;
import com.minsf.springbootinit.model.vo.QuestionSubmitVo;
import com.minsf.springbootinit.service.QuestionSubmitService;
import com.minsf.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子点赞接口
 *
@author <a href="http://bi.minsf.top">minify</a>
 */
//@RestController
//@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService postQuestionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 点赞 / 取消点赞
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
            HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 登录才能提交
        final User loginUser = userService.getLoginUser(request);
        Long questionSubmitId = postQuestionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(questionSubmitId);
    }

    @GetMapping("/get/vo")
    public BaseResponse<QuestionSubmitVo> getPostVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QuestionSubmit QuestionSubmit = postQuestionSubmitService.getById(id);
        if (QuestionSubmit == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(postQuestionSubmitService.grtQuestionSubmitVo(QuestionSubmit, request));
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param QuestionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVo>> listPostVOByPage(@RequestBody QuestionSubmitQueryRequest QuestionSubmitQueryRequest,
                                                       HttpServletRequest request) {
        long current = QuestionSubmitQueryRequest.getCurrent();
        long size = QuestionSubmitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> postPage = postQuestionSubmitService.page(new Page<>(current, size),
                postQuestionSubmitService.getQueryWrapper(QuestionSubmitQueryRequest));
        return ResultUtils.success(postQuestionSubmitService.getQuestionSubmitVoPage(postPage, request));
    }

    /**
     * 分页获取当前用户创建的资源列表
     *
     * @param QuestionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVo>> listMyPostVOByPage(@RequestBody QuestionSubmitQueryRequest QuestionSubmitQueryRequest,
                                                         HttpServletRequest request) {
        if (QuestionSubmitQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        QuestionSubmitQueryRequest.setUserId(loginUser.getId());
        long current = QuestionSubmitQueryRequest.getCurrent();
        long size = QuestionSubmitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> postPage = postQuestionSubmitService.page(new Page<>(current, size),
                postQuestionSubmitService.getQueryWrapper(QuestionSubmitQueryRequest));
        return ResultUtils.success(postQuestionSubmitService.getQuestionSubmitVoPage(postPage, request));
    }

}
